home *** CD-ROM | disk | FTP | other *** search
- /* Send and receive User Datagram Protocol packets */
- #include <stdlib.h>
- #include <string.h>
- #include "global.h"
- #include "mbuf.h"
- #include "misc.h"
- #include "netuser.h"
- #include "internet.h"
- #include "udp.h"
-
- static struct udp_cb *lookup_udp(struct socket *);
- static int16 hash_udp(struct socket *);
-
- /* Hash table for UDP structures */
- struct udp_cb *udps[NUDP] = { NULLUDP} ;
- struct udp_stat udp_stat; /* Statistics */
-
- /* Create a UDP control block for lsocket, so that we can queue
- * incoming datagrams.
- */
- int open_udp(struct socket *lsocket, void (*r_upcall)(), void *arg)
- {
- register struct udp_cb *up;
- int16 hval;
-
- if((up = lookup_udp(lsocket)) != NULLUDP)
- return 0; /* Already exists */
- if((up = (struct udp_cb *)malloc(sizeof(struct udp_cb))) == NULLUDP){
- net_error = NO_SPACE;
- return -1;
- }
- up->rcvq = NULLBUF;
- up->rcvcnt = 0;
- up->socket.address = lsocket->address;
- up->socket.port = lsocket->port;
- up->r_upcall = r_upcall;
- up->arg = arg;
-
- hval = hash_udp(lsocket);
- up->next = udps[hval];
- up->prev = NULLUDP;
- if(up->next != NULLUDP)
- up->next->prev = up;
- udps[hval] = up;
- return 0;
- }
-
- /* Send a UDP datagram */
- int send_udp(struct socket *lsocket, struct socket *fsocket,
- char tos, char ttl, struct mbuf *data,
- int16 length, int16 id, char df)
- {
- struct mbuf *bp;
- struct pseudo_header ph;
- struct udp udp;
-
- length = UDPHDR;
- if(data != NULLBUF)
- length += len_mbuf(data);
-
- udp.source = lsocket->port;
- udp.dest = fsocket->port;
- udp.length = length;
-
- /* Create IP pseudo-header, compute checksum and send it */
- ph.length = length;
- ph.source = lsocket->address;
- ph.dest = fsocket->address;
- ph.protocol = UDP_PTCL;
-
- if((bp = htonudp(&udp,data,&ph)) == NULLBUF){
- net_error = NO_SPACE;
- free_p(data);
- return 0;
- }
- udp_stat.sent++;
- ip_send(lsocket->address,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
- return length;
- }
-
- /* Accept a waiting datagram, if available. Returns length of datagram */
- int recv_udp(struct socket *lsocket, struct socket *fsocket, struct mbuf **bp)
- {
- register struct udp_cb *up;
- struct socket *sp;
- struct mbuf *buf;
- int16 length;
-
-
- up = lookup_udp(lsocket);
- if(up == NULLUDP){
- net_error = NO_CONN;
- return -1;
- }
- if(up->rcvcnt == 0){
- net_error = WOULDBLK;
- return -1;
- }
- buf = dequeue(&up->rcvq);
- up->rcvcnt--;
-
- sp = (struct socket *)buf->data;
- /* Fill in the user's foreign socket structure, if given */
- if(fsocket != NULLSOCK){
- fsocket->address = sp->address;
- fsocket->port = sp->port;
- }
- /* Strip socket header and hand data to user */
- pullup(&buf,NULLCHAR,sizeof(struct socket));
- length = len_mbuf(buf);
- if(bp != (struct mbuf **)NULL)
- *bp = buf;
- else
- free_p(buf);
- return length;
- }
- /* Delete a UDP control block */
- int del_udp(struct socket *lsocket)
- {
- register struct udp_cb *up;
- struct mbuf *bp;
- int16 hval;
-
- if((up = lookup_udp(lsocket)) == NULLUDP){
- net_error = INVALID;
- return -1;
- }
- /* Get rid of any pending packets */
- while(up->rcvcnt != 0){
- bp = up->rcvq;
- up->rcvq = up->rcvq->anext;
- free_p(bp);
- up->rcvcnt--;
- }
- hval = hash_udp(&up->socket);
- if(udps[hval] == up){
- /* First on list */
- udps[hval] = up->next;
- up->next->prev = NULLUDP;
- } else {
- up->prev->next = up->next;
- up->next->prev = up->prev;
- }
- free((char *)up);
- return 0;
- }
- /* Process an incoming UDP datagram */
- void udp_input(struct mbuf *bp, char protocol, int32 source,
- int32 dest, char tos, int16 length, char rxbroadcast)
- {
- struct pseudo_header ph;
- struct udp udp;
- struct udp_cb *up;
- struct socket lsocket;
- struct socket *fsocket;
- struct mbuf *packet;
- int ckfail = 0;
-
- tos = tos;
-
- if(bp == NULLBUF)
- return;
-
- udp_stat.rcvd++;
-
- /* Create pseudo-header and verify checksum */
- ph.source = source;
- ph.dest = dest;
- ph.protocol = protocol;
- ph.length = length;
-
- if(cksum(&ph,bp,length) != 0)
- /* Checksum apparently failed, note for later */
- ckfail++;
-
- /* Extract UDP header in host order */
- ntohudp(&udp,&bp);
-
- /* If the checksum field is zero, then ignore a checksum error.
- * I think this is dangerously wrong, but it is in the spec.
- */
- if(ckfail && udp.checksum != 0){
- udp_stat.cksum++;
- free_p(bp);
- return;
- }
- /* If this was a broadcast packet, pretend it was sent to us */
- if(rxbroadcast){
- lsocket.address = ip_addr;
- udp_stat.bdcsts++;
- } else
- lsocket.address = dest;
-
- lsocket.port = udp.dest;
- /* See if there's somebody around to read it */
- if((up = lookup_udp(&lsocket)) == NULLUDP){
- /* Nope, toss it on the floor */
- udp_stat.unknown++;
- free_p(bp);
- return;
- }
- /* Create space for the foreign socket info */
- if((packet = pushdown(bp,sizeof(struct socket))) == NULLBUF){
- /* No space, drop whole packet */
- free_p(bp);
- return;
- }
- fsocket = (struct socket *)packet->data;
- fsocket->address = source;
- fsocket->port = udp.source;
-
- /* Queue it */
- enqueue(&up->rcvq,packet);
- up->rcvcnt++;
- if(up->r_upcall)
- (*up->r_upcall)(&lsocket,up->rcvcnt,up->arg);
- }
- /* Look up UDP socket, return control block pointer or NULLUDP if nonexistant */
- static struct udp_cb *lookup_udp(struct socket *socket)
- {
- register struct udp_cb *up;
-
- up = udps[hash_udp(socket)];
-
- while (up != NULLUDP) {
- if (socket->address == up->socket.address &&
- socket->port == up->socket.port)
- break;
-
- up = up->next;
- }
- return up;
- }
-
- /* Hash a UDP socket (address and port) structure */
- static int16 hash_udp(struct socket *socket)
- {
- int16 hval;
-
- /* Compute hash function on socket structure */
- hval = hiword(socket->address);
- hval ^= loword(socket->address);
- hval ^= socket->port;
- hval %= NUDP;
- return hval;
- }
- /* Convert UDP header in internal format to an mbuf in external format */
- struct mbuf *htonudp(struct udp *udp, struct mbuf *data, struct pseudo_header *ph)
- {
- struct mbuf *bp;
- register char *cp;
- int16 checksum;
-
- /* Allocate UDP protocol header and fill it in */
- if((bp = pushdown(data,UDPHDR)) == NULLBUF)
- return NULLBUF;
-
- cp = bp->data;
- cp = put16(cp,udp->source); /* Source port */
- cp = put16(cp,udp->dest); /* Destination port */
- cp = put16(cp,udp->length); /* Length */
- *cp++ = 0; /* Clear checksum */
- *cp-- = 0;
-
- /* All zeros and all ones is equivalent in one's complement arithmetic;
- * the spec requires us to change zeros into ones to distinguish an
- * all-zero checksum from no checksum at all
- */
- if((checksum = cksum(ph,bp,ph->length)) == 0)
- checksum = (int16)0xffffffff;
- put16(cp,checksum);
- return bp;
- }
- /* Convert UDP header in mbuf to internal structure */
- void ntohudp(struct udp *udp, struct mbuf **bpp)
- {
- udp->source = pull16(bpp);
- udp->dest = pull16(bpp);
- udp->length = pull16(bpp);
- udp->checksum = pull16(bpp);
- }
-